﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using System.Threading;
using System.Collections;
using DevExpress.XtraGrid.Views.Grid;

namespace QLTV.Forms.KeHoach
{
    public partial class frmOverviewTrainingPlan : BaseForm
    {
        public frmOverviewTrainingPlan()
        {
            InitializeComponent();
            if (Program.language == "en")
            {
                bandYear.Caption = "Year " + DateTime.Now.Year.ToString();
            }
            else
            {
                bandYear.Caption = "Năm " + DateTime.Now.Year.ToString();
            }
           
        }
        private void frmOverviewTrainingPlan_Load(object sender, EventArgs e)
        {
            RunLongThread(loadCurrentYear);
        }
        private void loadCurrentYear()
        {

            this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
            tblShortPlanTableAdapter.returnUndonePLan(dsShortPlan.tblShortPlan);
            int count = (from c in tblTrainingOverViewTableAdapter.GetData().AsEnumerable()
                         where c.Field<int>("Nam") == DateTime.Now.Year
                         select c).Count();
            if (count > 0)
            {
                if (count == this.dsTraining.tblTrainingCourse.Rows.Count)
                    tblTrainingOverViewTableAdapter.FillByYear(dsTraining.tblTrainingOverView, DateTime.Now.Year);
                else
                {
                    tblTrainingOverViewTableAdapter.FillByYear(dsTraining.tblTrainingOverView, DateTime.Now.Year);
                    List<int> listCourse = (from c in tblTrainingOverViewTableAdapter.GetDataByYear(DateTime.Now.Year).AsEnumerable()
                                            select c.nTrainingCourse).ToList<int>();
                    //danh sach course chua cap nhat
                    List<int> listNewCourse = (from c in dsTraining.tblTrainingCourse.AsEnumerable()
                                               where !listCourse.Contains(c.nTrainingCourse)
                                               select c.nTrainingCourse).ToList();
                    foreach (int item in listNewCourse)
                    {
                        DataRow row = dsTraining.tblTrainingOverView.NewRow();
                        row["nTrainingCourse"] = item;
                        row["Nam"] = DateTime.Now.Year;
                        row["Scheduled"] = "";
                        dsTraining.tblTrainingOverView.Rows.Add(row);
                    }
                }
            }
            else
            {
                for (int i = 0; i < dsTraining.tblTrainingCourse.Rows.Count; i++)
                {
                    DataRow row = dsTraining.tblTrainingOverView.NewRow();
                    row["nTrainingCourse"] = dsTraining.tblTrainingCourse.Rows[i]["nTrainingCourse"];
                    row["Nam"] = DateTime.Now.Year;
                    row["Scheduled"] = "";
                    dsTraining.tblTrainingOverView.Rows.Add(row);
                }
            }

            _wait.ShouldCloseNow = true;

        }
        int count = 1;
        private void viewTrainingPlan_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {

            if (e.RowHandle > -1)
            {

                if (count < viewTrainingPlan.RowCount * 12 + 1)
                {

                    if (e.Column == gridColumn1 || e.Column == gridColumn2 || e.Column == gridColumn3 || e.Column == gridColumn4 || e.Column == gridColumn5 || e.Column == gridColumn6
                            || e.Column == gridColumn7 || e.Column == gridColumn8 || e.Column == gridColumn9 || e.Column == gridColumn10 || e.Column == gridColumn11 || e.Column == gridColumn12)
                    {

                        //tháng cần so sánh trong Assignment và kế hoạch ngắn hạn
                        int thang = Convert.ToInt32(e.Column.Caption);
                        int nam = DateTime.Now.Year;
                        //mã môn học 
                        int CourseID = Convert.ToInt32(viewTrainingPlan.GetRowCellValue(e.RowHandle, colID));
                        //Chức danh cho khóa học này
                        List<int> listRankIDForThisCourse = (from item in tblRankCourseTableAdapter1.GetDataByCourse(CourseID).AsEnumerable()
                                                             select item.Field<int>("nRankId")).ToList<int>();
                        //kiem tra tv da hoc khoa nay chua?
                        Database.dsTrainingTableAdapters.tblTrainingCrewsTableAdapter tadTrainingCrew = new Database.dsTrainingTableAdapters.tblTrainingCrewsTableAdapter();
                        List<string> listCrew = (from item in tadTrainingCrew.GetData().AsEnumerable()
                                                 where item.nTrainingCourse == CourseID
                                                 select item.nCrewId).ToList<string>();

                        Database.dsTrainingTableAdapters.tadOverview tadOver = new Database.dsTrainingTableAdapters.tadOverview();

                        tadOver.Fill(dsTraining.tblOverview, new DateTime(nam, thang, 1));
                        string q = (from a in dsTraining.tblOverview.AsEnumerable()
                                    where listRankIDForThisCourse.Contains(a.nRankID) && !listCrew.Contains(a.nCrewid)
                                    select a).Count().ToString();

                        viewTrainingPlan.SetRowCellValue(e.RowHandle, e.Column, q);
                        count++;
                    }
                    else
                        return;
                }
                else
                {
                    tblTrainingOverViewBindingSource.EndEdit();
                    tblTrainingOverViewTableAdapter.Update(dsTraining.tblTrainingOverView);
                }
            }
        }
        private void viewTrainingPlan_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {

            GridView currentView = sender as GridView;
            try
            {
                if (e.Column == gridColumn1 || e.Column == gridColumn2 || e.Column == gridColumn3 || e.Column == gridColumn4 || e.Column == gridColumn5 || e.Column == gridColumn6
                    || e.Column == gridColumn7 || e.Column == gridColumn8 || e.Column == gridColumn9 || e.Column == gridColumn10 || e.Column == gridColumn11 || e.Column == gridColumn12)
                {
                    List<string> listScheduled = currentView.GetRowCellValue(e.RowHandle, colScheduled).ToString().Split(',').ToList<string>();
                    //tháng cần so sánh trong Assignment và kế hoạch ngắn hạn
                    int thang = Convert.ToInt32(e.Column.Caption);
                    int nam = DateTime.Now.Year;
                    //mã môn học 
                    int CourseID = Convert.ToInt32(currentView.GetRowCellValue(e.RowHandle, colID));
                    //Chức danh cho khóa học này
                    List<int> listRankIDForThisCourse = (from item in tblRankCourseTableAdapter1.GetDataByCourse(CourseID).AsEnumerable()
                                                         select item.Field<int>("nRankId")).ToList<int>();
                    //TV chua tham gia khoa hoc nay
                    Database.dsTrainingTableAdapters.tblTrainingCrewsTableAdapter tadTrainingCrew = new Database.dsTrainingTableAdapters.tblTrainingCrewsTableAdapter();
                    List<string> listCrew = (from item in tadTrainingCrew.GetData().AsEnumerable()
                                             where item.nTrainingCourse == CourseID
                                             select item.nCrewId).ToList<string>();
                    //TV ko co trong ke hoach ngan han
                    IList listShortPlan = (from item in dsShortPlan.tblShortPlan.AsEnumerable()
                                           select new
                                           {
                                               crewID = (item.IsnCrewIDNextNull() ? null : item.nCrewIDNext)
                                           }).ToList();

                    Database.dsTrainingTableAdapters.tadOverview tadOver = new Database.dsTrainingTableAdapters.tadOverview();
                    tadOver.Fill(dsTraining.tblOverview, new DateTime(nam, thang, 1));

                    frmTrainingCrew c = new frmTrainingCrew();
                    IList query = (from a in dsTraining.tblOverview
                                   where listRankIDForThisCourse.Contains(a.nRankID) && !listCrew.Contains(a.nCrewid) && !listShortPlan.Contains(a.nCrewid)
                                   select new
                                   {
                                       nCrewid = a.nCrewid,
                                       nRankID = a.nRankID,
                                       FullName = String.Format("{0} {1} {2}", a.cFamilyName, a.cMiddleName, a.cFirstName),
                                       cRankName = a.cRankName,
                                       cStatus = a.cStatus,
                                       cHomeNo = (a.IscHomeNoNull() ? null : a.cHomeNo),
                                       cHandNo = (a.IscHandNoNull() ? null : a.cHandNo),
                                       cVesselName = (a.IscVesselNameNull() ? null : a.cVesselName),
                                       dDuKienRoiTau = (a.IsdDuKienRoiTauNull() ? null : a.dDuKienRoiTau.ToString("dd/MM/yyyy")),
                                       dSanSangLenTau = (a.IsdSanSangLenTauNull() ? null : a.dSanSangLenTau.ToString("dd/MM/yyyy"))
                                   }).ToList();

                    c.datasourse = query;
                    c.thang = thang;
                    c.nam = nam;
                    c.courseID = CourseID;
                    if (listScheduled.Contains(e.Column.Caption))
                        c.isNew = false;
                    else
                        c.isNew = true;
                    if (Convert.ToInt16(e.Column.Caption) < DateTime.Now.Month)
                    {
                        c.btnSave.Enabled = false;
                    }
                    c.ShowDialog();
                    loadCurrentYear();
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void viewTrainingPlan_RowStyle(object sender, RowStyleEventArgs e)
        {

        }

        private void viewTrainingPlan_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {

            GridView View = sender as GridView;
            if (e.RowHandle >= 0)
            {
                if (e.Column == gridColumn1 || e.Column == gridColumn2 || e.Column == gridColumn3 || e.Column == gridColumn4 || e.Column == gridColumn5 || e.Column == gridColumn6
                         || e.Column == gridColumn7 || e.Column == gridColumn8 || e.Column == gridColumn9 || e.Column == gridColumn10 || e.Column == gridColumn11 || e.Column == gridColumn12)
                {
                    if (Convert.ToInt16(e.Column.Caption) < DateTime.Now.Month)
                    {
                        e.Appearance.BackColor = Color.Gainsboro;
                    }
                    //////string scheduler
                    List<string> listScheduled = View.GetRowCellValue(e.RowHandle, colScheduled).ToString().Split(',').ToList<string>();
                    //////neu khoa hoc nay da duoc dat lich thi se khong update thong tin thuyen vien cho khoa hoc nay
                    if (listScheduled.Contains(e.Column.Caption))
                    {

                        e.Appearance.BackColor = Color.MediumSpringGreen;
                    }
                }
            }
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
    }

}